package qq.android.wan.qq.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import qq.android.wan.qq.R;
import qq.android.wan.qq.adapter.SearchAdapter;
import qq.android.wan.qq.chat.activity.ChatActivity;
import qq.android.wan.qq.db.dao.FriendDao;
import qq.android.wan.qq.entity.FriendBean;

/**
 * Created by wxx on 2016/4/6.
 */
public class SearchActivity extends Activity {
    private List<FriendBean> datas;
    private EditText et_search;
    private ListView lv_query_result;
    private Button btn_cancle;
    private List<FriendBean> filters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initUI();
        initListener();
        FriendDao friendDao=new FriendDao(this);
        datas=friendDao.getAllFriends();
    }
    private void initUI() {
        et_search = (EditText) findViewById(R.id.et_search);
        lv_query_result = (ListView) findViewById(R.id.lv_query_result);
        btn_cancle = (Button) findViewById(R.id.btn_cancle);
        lv_query_result.setEmptyView(findViewById(R.id.layout_empty));
    }
    private void initListener() {
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                filters = new ArrayList<FriendBean>();
                if (!s.toString().equals("")) {
                    for (int i = 0; i < datas.size(); i++) {
                        if ((datas.get(i).getUserId()+"").contains(s)||datas.get(i).getNick().contains(s)) {
                            filters.add(datas.get(i));
                        }
                    }
                    lv_query_result.setEmptyView(findViewById(R.id.layout_nodata));
                } else {
                    lv_query_result.setEmptyView(findViewById(R.id.layout_empty));
                }
                lv_query_result.setAdapter(new SearchAdapter(SearchActivity.this, filters, s.toString()));
            }
        });
        lv_query_result.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchActivity.this, ChatActivity.class);
                intent.putExtra("userId", filters.get(position).getUserId());
                intent.putExtra("nick", "奋斗青少年");
                startActivity(intent);
            }
        });
    }


}
